package com.fr.data.core.db.dml;

import com.fr.base.FRContext;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Update extends InsertOrUpdate
{
  private Where where;

  public Update(Table paramTable)
  {
    this(paramTable, DialectFactory.getDefaultDialect());
  }

  public Update(Table paramTable, Dialect paramDialect)
  {
    this(paramTable, null, paramDialect);
  }

  public Update(Table paramTable, Where paramWhere, Dialect paramDialect)
  {
    super(paramTable, paramDialect);
    this.where = paramWhere;
  }

  public boolean isUpdate()
  {
    return true;
  }

  public Where getWhere()
  {
    return this.where;
  }

  public void setWhere(Where paramWhere)
  {
    this.where = paramWhere;
  }

  public static boolean validate(Update paramUpdate)
  {
    return ((paramUpdate != null) && (paramUpdate.getColumnCount() > 0));
  }

  public PreparedStatement createPreparedStatement(Connection paramConnection)
    throws SQLException
  {
    if (paramConnection == null)
    {
      FRContext.getLogger().log(Level.WARNING, "Connection can not be null.");
      return null;
    }
    if (getTable() == null)
    {
      FRContext.getLogger().log(Level.WARNING, "Table can not be null");
      return null;
    }
    if (!(validate(this)))
    {
      FRContext.getLogger().log(Level.WARNING, "Update column can not be null");
      return null;
    }
    if ((!(isEmptyWhereAllowed())) && (this.where == null))
    {
      FRContext.getLogger().log(Level.WARNING, "Where can not be null");
      return null;
    }
    if (this.dialect == null)
      this.dialect = DialectFactory.getDefaultDialect();
    int i = getColumnCount();
    String[] arrayOfString = new String[i];
    for (int j = 0; j < i; ++j)
      arrayOfString[j] = this.dialect.column2SQL(getColumnName(j)) + "=?";
    String str1 = getTable().toStatementSQLString(this.dialect);
    String str2 = "";
    String str3 = (this.where == null) ? "" : this.where.toStatementSQLString(this.dialect);
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(" ");
    int k = 0;
    int l = arrayOfString.length;
    while (k < l)
    {
      localStringBuffer.append(arrayOfString[k]);
      if (k < l - 1)
        localStringBuffer.append(",");
      ++k;
    }
    localStringBuffer.append(" ");
    str2 = localStringBuffer.toString();
    String str4 = "UPDATE " + str1 + " SET" + str2 + str3;
    FRContext.getLogger().log(Level.INFO, str4);
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement(str4.toString());
    for (int i1 = 0; i1 < i; ++i1)
      DBUtils.applyColumnTypeValue(this.dialect, paramConnection, getTable(), getColumnName(i1), localPreparedStatement, i1 + 1, getColumnType(i1), getColumnValue(i1));
    if (this.where != null)
      this.where.applyColumnTypeValue(this.dialect, paramConnection, getTable(), localPreparedStatement, i + 1);
    return localPreparedStatement;
  }

  public int execute(Connection paramConnection)
    throws SQLException
  {
    PreparedStatement localPreparedStatement = createPreparedStatement(paramConnection);
    try
    {
      if (localPreparedStatement != null)
      {
        int i = localPreparedStatement.executeUpdate();
        return i;
      }
    }
    finally
    {
      DBUtils.closeStatement(localPreparedStatement);
    }
    return 0;
  }
}